﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Lucene.Net.Analysis;
using Lucene.Net.Analysis.Cn;
using Lucene.Net.Documents;
using Lucene.Net.Index;

namespace LuceneIndex
{
    /// <summary>
    /// 基于DotLucene建立数据库索引
    /// </summary>
    public class DbFtpIndexer
    {
        private string _connString = "";   // 数据库连接字符串
        private IndexWriter _indexWriter = null;   // DotLucene索引器
        private Analyzer _analyzer = null;    // DotLucene分词器
        private string _indexDir = "";   // 索引目录

        private int _count;   // 索引记录个数
        private TimeSpan _elapsedTime; // 花费时间

        public DbFtpIndexer()
        {

        }

        public DbFtpIndexer(string connString, string indexDir)
        {
            this.ConnString = connString;
            this._indexDir = indexDir;
        }

        public string ConnString
        {
            get { return _connString; }
            set { _connString = value; }
        }

        public int Count
        {
            get { return _count; }
            set { _count = value; }
        }

        public TimeSpan ElapsedTime
        {
            get { return _elapsedTime; }
            set { _elapsedTime = value; }
        }

        public string IndexDir
        {
            get { return _indexDir; }
            set { _indexDir = value; }
        }

        public void Initialize()
        {
            this._analyzer = new ChineseAnalyzer();
            this._indexWriter = new IndexWriter(this._indexDir, this._analyzer, true);
        }

        public void Index()
        {
            DateTime startTime = DateTime.Now;    // 开始时间
            this.Count = 0;

            this.Initialize();

            Document doc = null;

            string strQuery = "SELECT id,is_file,filename,access_mode,num_links,owner,[group],size,path,modified_time FROM t_fileinfo";
            using (SqlConnection conn = new SqlConnection(this._connString))
            {
                using (SqlCommand cmd = new SqlCommand(strQuery, conn))
                {
                    conn.Open();
                    SqlDataReader dr = cmd.ExecuteReader();
                    while (dr.Read())
                    {
                        doc = new Document();
                        doc.Add(new Field("Id", dr.GetInt32(0).ToString(), Field.Store.YES, Field.Index.NO));
                        doc.Add(new Field("IsFile", dr.GetInt16(1).ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
                        doc.Add(new Field("Filename", dr.GetString(2), Field.Store.YES, Field.Index.TOKENIZED));
                        doc.Add(new Field("AccessMode", dr.GetString(3), Field.Store.YES, Field.Index.NO));
                        doc.Add(new Field("NumLinks", dr.GetInt32(4).ToString(), Field.Store.YES, Field.Index.NO));
                        doc.Add(new Field("Owner", dr.GetString(5), Field.Store.YES, Field.Index.NO));
                        doc.Add(new Field("Group", dr.GetString(6), Field.Store.YES, Field.Index.NO));
                        doc.Add(new Field("Size", dr.GetInt64(7).ToString(), Field.Store.YES, Field.Index.UN_TOKENIZED));
                        doc.Add(new Field("Path", dr.GetString(8), Field.Store.YES, Field.Index.TOKENIZED));
                        doc.Add(new Field("ModifiedTime", dr.GetDateTime(9).ToLongTimeString(), Field.Store.YES, Field.Index.UN_TOKENIZED));

                        this._indexWriter.AddDocument(doc);

                        this.Count++;

                        // Console.WriteLine("索引文件：{0}完成", dr.GetString(8) + dr.GetString(2));
                    }
                }
            }

            this._indexWriter.Close();

            DateTime endTime = DateTime.Now;

            this.ElapsedTime = endTime - startTime;

            Console.WriteLine("创建索引完成，共费时间：{0}", this.ElapsedTime);
        }
    }
}
